package com.letoken.platform.pub.log;

import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 用于非web请求
@Configuration
public class MdcTraceIdConfiguration {
    @Value("${log.traceId.pointcutExpression:execution(* com.*.*.job(..))}")
    private String POINTCUT_EXPRESSION;

    @Value("${log.traceId.handlerExpression:execution(* com.*.*.job(..))}")
    private String HANDLER_EXPRESSION;

    @Bean("MdcTraceIdMethodInterceptor")
    public MdcTraceIdMethodInterceptor mdcTraceIdMethodInterceptor() {
        return new MdcTraceIdMethodInterceptor();
    }

    @Bean("mdcTraceIdAdvisor")
    public Advisor mdcTraceIdAdvisor(MdcTraceIdMethodInterceptor mdcTraceIdMethodInterceptor) {
        AspectJExpressionPointcut cut = new AspectJExpressionPointcut();
        cut.setExpression(POINTCUT_EXPRESSION);
        Advisor advisor = new DefaultPointcutAdvisor(cut, mdcTraceIdMethodInterceptor);
        return advisor;
    }


    @Bean("mqHandlerAdvisor")
    public Advisor mqHandlerAdvisor(MdcTraceIdMethodInterceptor mdcTraceIdMethodInterceptor) {
        AspectJExpressionPointcut cut = new AspectJExpressionPointcut();
        cut.setExpression(HANDLER_EXPRESSION);
        Advisor advisor = new DefaultPointcutAdvisor(cut, mdcTraceIdMethodInterceptor);
        return advisor;
    }

}
